package org.scboy.rbac.dubbo;

import org.apache.dubbo.config.annotation.DubboService;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.api.IRbacManager;
import org.scboy.rbac.api.entity.LoginState;
import org.scboy.rbac.dao.RbacLoginSessionDao;
import org.scboy.rbac.dao.RbacLoginStateDao;
import org.scboy.rbac.loads.loadLoginUser;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.rbac.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class IRbacManagerImpl implements IRbacManager{

	@Autowired
	EntityFactory factory;
	
	@Autowired
	loadLoginUser login;
	
	@Override
	public LoginState getLoginState(String token) {
		RbacLoginStateDao dao=factory.getDao(RbacLoginStateDao.class);
		Model loginState=dao.get(Model.create().add("sessionId", token));
		if(loginState==null) {
			return null;
		}else {
			return loginState.toEntity(LoginState.class);
		}
	}

	@Override
	public LoginUser checkLogin(String token) {
		return login.get(token);
	}

	@Override
	public boolean checkAuth(String userId, String path) {
		RbacLoginSessionDao dao=factory.getDao(RbacLoginSessionDao.class);
		long count=dao.checkAuth(Model.create().add("permUrl", path).add("userId", userId));
		if(count>0) {
			return true;
		}else {
			return false;
		}
	}
}
